﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace ScriptPacker.Core
{
	public  static partial class ObjExtensions
	{
		public static void MoveListViewItem(this ListView lv, bool moveUp,Action<ListView> callback)
		{
			if (lv.SelectedItems.Count == 0) return;
			string cache;
			int selIdx;

			selIdx = lv.SelectedItems[0].Index;
			if (moveUp)
			{
				// ignore moveup of row(0)
				if (selIdx == 0)
					return;

				// move the subitems for the previous row
				// to cache to make room for the selected row
				for (int i = 0; i < lv.Items[selIdx].SubItems.Count; i++)
				{
					cache = lv.Items[selIdx - 1].SubItems[i].Text;
					lv.Items[selIdx - 1].SubItems[i].Text =
					  lv.Items[selIdx].SubItems[i].Text;
					lv.Items[selIdx].SubItems[i].Text = cache;
				}
				lv.Items[selIdx - 1].Selected = true;
				lv.Items[selIdx].Selected = false;
				lv.Refresh();
				lv.Focus();
			}
			else
			{
				// ignore movedown of last item
				if (selIdx == lv.Items.Count - 1)
					return;
				// move the subitems for the next row
				// to cache so we can move the selected row down
				for (int i = 0; i < lv.Items[selIdx].SubItems.Count; i++)
				{
					cache = lv.Items[selIdx + 1].SubItems[i].Text;
					lv.Items[selIdx + 1].SubItems[i].Text =
					  lv.Items[selIdx].SubItems[i].Text;
					lv.Items[selIdx].SubItems[i].Text = cache;
				}
				lv.Items[selIdx + 1].Selected = true;
				lv.Items[selIdx].Selected = false;
				lv.Refresh();
				lv.Focus();
			}
			if (callback != null) {
				callback(lv);
			};
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="assemblyName">assembly name</param>
		/// <param name="fileName">full file name.[defaultnamespace]+"."+[path to the file].E.G:ScriptPacker.Assets.img.xx.gif</param>
		/// <returns></returns>
		public static Stream GetEmbeddedFile(string assemblyName, string fileName) {
			try
			{
				Assembly a = Assembly.Load(assemblyName);
				var folderPath = string.Empty;
				fileName = fileName.Replace(@"\", ".");
				Stream str = a.GetManifestResourceStream(fileName);

				if (str == null)
					throw new Exception(String.Format("Could not locate embedded resource '{0}' in assembly '{1}'", fileName, assemblyName));
				return str;
			}
			catch (Exception e)
			{
				throw;
			}

		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="ass"></param>
		/// <param name="fileName">full file name.[defaultnamespace]+"."+[path to the file].E.G:ScriptPacker.Assets.img.xx.gif</param>
		/// <returns></returns>
		public static Stream GetEmbeddedFile(this Assembly ass, string fileName) {
			string assemblyName = ass.GetName().Name;
			return GetEmbeddedFile(assemblyName, fileName);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="type"></param>
		/// <param name="fileName">full file name.[defaultnamespace]+"."+[path to the file].E.G:ScriptPacker.Assets.img.xx.gif</param>
		/// <returns></returns>
		public static Stream GetEmbeddedFile(this Type type, string fileName) {
			return type.Assembly.GetEmbeddedFile(fileName);
		}

		public static Form ResizeWith(this Form src,int rawHeight,int rawWidth,params Control[] follow) {
			if (follow == null || follow.Length == 0) return src;
			var hRate = (Convert.ToDouble(src.Height) / Convert.ToDouble(rawHeight));
			var wRate=(Convert.ToDouble(src.Width) / Convert.ToDouble(rawWidth));
			follow.ToList().ForEach(x => {
				x.Height =(int)(hRate*x.Height);
				x.Width = (int)(wRate*x.Width);
				x.Top = (int)(hRate*x.Top);
				x.Left = (int)(wRate*x.Left);
			});
			
			return src;
		}
	}
}
